package com.ruoyi.qiushi.service.system.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.qiushi.domain.system.QiushiTask;
import com.ruoyi.qiushi.mapper.system.QiushiTaskMapper;
import com.ruoyi.qiushi.service.system.IQiushiTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 任务Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-29
 */
@Service
public class QiushiTaskServiceImpl implements IQiushiTaskService {
    @Autowired
    private QiushiTaskMapper qiushiTaskMapper;

    /**
     * 查询任务
     *
     * @param id 任务主键
     * @return 任务
     */
    @Override
    public QiushiTask selectQiushiTaskById(Long id) {
        return qiushiTaskMapper.selectQiushiTaskById(id);
    }

    /**
     * 查询任务列表
     *
     * @param qiushiTask 任务
     * @return 任务
     */
    @Override
    public List<QiushiTask> selectQiushiTaskList(QiushiTask qiushiTask) {
        return qiushiTaskMapper.selectQiushiTaskList(qiushiTask);
    }

    /**
     * 新增任务
     *
     * @param qiushiTask 任务
     * @return 结果
     */
    @Override
    public int insertQiushiTask(QiushiTask qiushiTask) {
        qiushiTask.setCreateTime(DateUtils.getNowDate());
        return qiushiTaskMapper.insertQiushiTask(qiushiTask);
    }

    /**
     * 修改任务
     *
     * @param qiushiTask 任务
     * @return 结果
     */
    @Override
    public int updateQiushiTask(QiushiTask qiushiTask) {
        qiushiTask.setUpdateTime(DateUtils.getNowDate());
        return qiushiTaskMapper.updateQiushiTask(qiushiTask);
    }

    /**
     * 删除任务信息
     *
     * @param id 任务主键
     * @return 结果
     */
    @Override
    public int deleteQiushiTaskById(Long id) {
        return qiushiTaskMapper.deleteQiushiTaskById(id);
    }

    /**
     * 获取今日任务完成次数
     * @param taskId
     * @param userId
     * @return
     */
    @Override
    public int getTodayTaskFinishCount(Long taskId,Long userId){
        return qiushiTaskMapper.selectTodayTaskFinishCount(userId,taskId);
    }

    @Override
    public long getTaskFinishCount(Long taskId, Long userId) {
        return qiushiTaskMapper.getTaskFinishCount(userId,taskId);
    }

    /**
     * 根据任务标识查询任务 ，并返回未完成任务
     *
     * @param flag
     * @return
     */
    @Override
    public List<QiushiTask> getNotFinishTaskByFlag(String flag, Long userId) {
        return this.getNotFinishTaskByFlag(flag,userId,null);
    }

    /**
     * 根据任务标识查询任务 ，并返回未完成任务
     * @param flag
     * @param userId
     * @param joinId
     * @return
     */
    @Override
    public List<QiushiTask> getNotFinishTaskByFlag(String flag, Long userId,Long joinId) {
        QiushiTask params = new QiushiTask();
        params.setFlag(flag);
        params.setDelFlag("0");
        params.setJoinId(joinId);
        List<QiushiTask> list = this.selectQiushiTaskList(params);

        List<QiushiTask> result = new ArrayList<>();
        for (QiushiTask task : list) {
            if ("1".equals(task.getType())) { //日常任务
                int count = this.getTodayTaskFinishCount(task.getId(), userId);
                if (count < 1) {
                    result.add(task);
                }
            } else if ("2".equals(task.getType())) {
                long count = this.getTaskFinishCount(task.getId(), userId);
                if (count < 1) {
                    result.add(task);
                }
            }
        }
        return result;
    }
}
